<?php
namespace Admin\Controller;
use Think\Controller;
class CatController extends CommonController {
	//类列表
    public function catelist()
    {
        //取出所有分类
        $cats = M('cat')->select();
        $cats = gettree($cats);
        $this->assign('cats',$cats);


        $this->display();
    }

    //添加显示
    public function cateadd()
    {//Array ( [cat_name] => x [parent_id] => 0 [intro] => x )
        if ($_POST) {
            $data['cat_name'] = I('post.cat_name');
            $data['parent_id'] = I('post.parent_id');
            $data['intro'] = I('post.intro');

            //去重
            $cat_name = M('cat')->where(array('parent_id'=>0,'cat_name'=>$data['cat_name']))->find();
            if ($cat_name) {
                $arr = array(
                    'error' => 0,
                    'info' => '已存在此分类名'
                    );
                echo json_encode($arr);
                exit;
            }

            //入库
            $res = M('cat')->add($data);
            if ($res) {
                $url = U('admin/cat/catelist');
                $arr = array(
                    'error' => 1,
                    'info' => '添加成功',
                    'url' => $url
                    );
                echo json_encode($arr);
            }else {
                $arr = array(
                    'error' => 0,
                    'info' => '添加失败'
                    );
                echo json_encode($arr);
            }

        }else{
            //类
            $cats = M('cat')->select();
            $cats = gettree($cats);
            $this->assign('cats',$cats);

            $this->display();
        }  
    }

    //类 编辑
    public function catedit()
    {
        if (IS_POST) {
            $data['cat_name'] = I('post.cat_name');
            $data['parent_id'] = I('post.parent_id');
            $data['intro'] = I('post.intro');
            //编辑
            $cat_id = I('post.cat_id');
            $res = M('cat')->where(array('cat_id'=>$cat_id))->save($data);
            if ($res) {
                $arr = array(
                    'error' => 1,
                    'info' => '更新成功'
                    );
                echo json_encode($arr);
            }else {
                $arr = array(
                    'error' => 0,
                    'info' => '更新失败'
                    );
                echo json_encode($arr);
            }

        }else{

            //类
            $cats = M('cat')->select();
            $cats = gettree($cats);
            $this->assign('cats',$cats);

            $cat_id = I('get.cat_id');
            $cat_info = M('cat')->where(array('cat_id'=>$cat_id))->find();
            $this->assign('cat_info',$cat_info);
            
            $this->display();
        }
    }

    //类  删除
    public function catedel()
    {
        $cat_id = I('post.cat_id');
        $res = M('cat')->delete($cat_id);
        if ($res) {
            $arr = array(
                'error' => 1,
                'info' => '删除成功'
                );
            echo json_encode($arr);
        }else {
            $arr = array(
                'error' => 0,
                'info' => '删除失败'
                );
            echo json_encode($arr);
        }
    }






}